package com.coffee.order.service.inquire.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.inquire.query.InquireOrderContractQuery;
import com.coffee.order.api.inquire.result.InquireOrderContractResult;
import com.coffee.order.api.inquire.service.InquireOrderContractService;
import com.coffee.order.service.inquire.bean.InquireOrderContract;
import com.coffee.order.service.inquire.dao.InquireOrderContractDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class InquireOrderContractServiceImpl implements InquireOrderContractService {
	@Resource
	private InquireOrderContractDao	inquireOrderContractDao;

	@Override
	public Page<InquireOrderContractResult> getPager(InquireOrderContractQuery param, PagerInfo pr) {
		Page<InquireOrderContractResult> pager=new Page<InquireOrderContractResult>(pr.getStart(), pr.getPageSize());
		Integer count=inquireOrderContractDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<InquireOrderContract> list=inquireOrderContractDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, InquireOrderContractResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(InquireOrderContractQuery param){
		Integer count=inquireOrderContractDao.count(param);
		return count;
	}
	
	@Override
	public InquireOrderContractResult getDetail(Integer id){
		InquireOrderContract item=inquireOrderContractDao.findById(id);
		if(item==null){
			return null;
		}
		InquireOrderContractResult result=new InquireOrderContractResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<InquireOrderContractResult> getList(InquireOrderContractQuery param, PagerInfo pr){
		List<InquireOrderContract> list=inquireOrderContractDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, InquireOrderContractResult.class);
	}
	
	@Override
	public List<InquireOrderContractResult> getList(InquireOrderContractQuery param){
		List<InquireOrderContract> list=inquireOrderContractDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireOrderContractResult.class);
	}
	
	@Override
	public List<InquireOrderContractResult> getList(){
		InquireOrderContractQuery param = new InquireOrderContractQuery();
		List<InquireOrderContract> list=inquireOrderContractDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireOrderContractResult.class);
	}


	@Override
	public void delete(Integer id) {
		inquireOrderContractDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(InquireOrderContractResult result)throws BusinessException {
		InquireOrderContract item=new InquireOrderContract();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			inquireOrderContractDao.insert(item);
		}else {
			InquireOrderContract tmp=inquireOrderContractDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			inquireOrderContractDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,InquireOrderContractResult> getItemMap(Set<Integer> idSet){
		Map<Integer, InquireOrderContractResult> map=new HashMap<Integer, InquireOrderContractResult>();
		List<InquireOrderContract> list=inquireOrderContractDao.findByIds(StringUtil.setToList(idSet));
		List<InquireOrderContractResult> resultList=DTOUtils.convertList(list, InquireOrderContractResult.class);
		for(InquireOrderContractResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,InquireOrderContractResult> getItemMap(){
		Map<Integer, InquireOrderContractResult> map=new HashMap<Integer, InquireOrderContractResult>();
		InquireOrderContractQuery param=new InquireOrderContractQuery();
		List<InquireOrderContract> list=inquireOrderContractDao.page(param, 0, Integer.MAX_VALUE);
		List<InquireOrderContractResult> resultList=DTOUtils.convertList(list, InquireOrderContractResult.class);
		for(InquireOrderContractResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
